<?php

include FCPATH . 'application/controllers/backend/backend.php';

class Jobs extends Backend {

    public function __construct() {
        parent::__construct();

        $this->load->model('jobs_model', 'model');
    }

    public function index() {
        $this->load_javascripts('backend_jobs');
        $this->rend('backend/jobs_index_view');
    }

    public function records() {
        $page = intval($this->input->get('page'));
        $page_size = intval($this->input->get('pageSize'));
        $offset = ($page - 1) * $page_size;

        $this->json_back('XT001', '', array(
            'total' => $this->model->total(),
            'records' => $this->model->records($page_size, $offset)
        ));
    }

    public function find() {
        $id = intval($this->uri->segment(4));

        $data = $this->model->find($id);

        $this->json_back('XT001', '', $data);
    }

    public function insert() {
        $data = array(
            'position' => trim($this->input->post('position')),
            'position_en' => trim($this->input->post('position_en')),
            'requirements' => trim($this->input->post('requirements')),
            'requirements_en' => trim($this->input->post('requirements_en')),
            'created_at' => date('Y-m-d H:i:s', time())
        );

        if ($this->model->insert($data)) {
            $this->json_back('XT001', '添加成功');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));

        $data = array(
            'position' => trim($this->input->post('position')),
            'position_en' => trim($this->input->post('position_en')),
            'requirements' => trim($this->input->post('requirements')),
            'requirements_en' => trim($this->input->post('requirements_en'))
        );

        if ($this->model->update($id, $data)) {
            $this->json_back('XT001', '更新成功');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function delete() {
        $id = intval($this->uri->segment(4));

        if ($this->model->delete($id)) {
            $this->json_back('XT001', '删除成功');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

}
